import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/login',
    component: () => import('../views/login/index.vue'),
    hidden: true
  },
  {
    path: '/',
    component: () => import('../layout/index.vue'),
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('../views/dashboard/index.vue'),
        meta: { title: '首页', icon: 'House' }
      }
    ]
  },
  {
    path: '/employees',
    component: () => import('../layout/index.vue'),
    redirect: '/employees/list',
    meta: { title: '员工管理', icon: 'User' },
    children: [
      {
        path: 'list',
        name: 'EmployeeList',
        component: () => import('../views/employees/index.vue'),
        meta: { title: '员工管理', icon: 'User' }
      }
    ]
  },
  {
    path: '/flowers',
    component: () => import('../layout/index.vue'),
    redirect: '/flowers/list',
    meta: { title: '鲜花管理', icon: 'ShoppingBag' },
    children: [
      {
        path: 'list',
        name: 'FlowerList',
        component: () => import('../views/flowers/index.vue'),
        meta: { title: '鲜花管理', icon: 'ShoppingBag' }
      }
    ]
  },
  {
    path: '/creative-products',
    component: () => import('../layout/index.vue'),
    redirect: '/creative-products/list',
    meta: { title: '文创管理', icon: 'Present' },
    children: [
      {
        path: 'list',
        name: 'CreativeProductList',
        component: () => import('../views/creative-products/index.vue'),
        meta: { title: '文创管理', icon: 'Present' }
      }
    ]
  },
  {
    path: '/orders',
    component: () => import('../layout/index.vue'),
    redirect: '/orders/list',
    meta: { title: '订单管理', icon: 'List' },
    children: [
      {
        path: 'list',
        name: 'OrderList',
        component: () => import('../views/orders/index.vue'),
        meta: { title: '订单管理', icon: 'List' }
      }
    ]
  },
  {
    path: '/customers',
    component: () => import('../layout/index.vue'),
    redirect: '/customers/list',
    meta: { title: '客户管理', icon: 'UserFilled' },
    children: [
      {
        path: 'list',
        name: 'CustomerList',
        component: () => import('../views/customers/index.vue'),
        meta: { title: '客户管理', icon: 'UserFilled' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.path === '/login') {
    if (token) {
      next('/')
    } else {
      next()
    }
  } else {
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router 